package com.yint.tree;

/**
 * 直到失败->装饰器节点
 * <p>
 * Created by 殷田 on 2018/5/3.
 */
public class UntilFailed extends Decorator {

    public UntilFailed() {
    }

    public UntilFailed(Leaf sub) {
        setSub(sub);
    }

    @Override
    protected synchronized STATUS onTick(Tree tree) {
        Leaf sub = getSub();
        if (sub == null) {
            return STATUS.SUCCEED;
        }
        if (sub.entry(tree)) {
            STATUS state = sub.tick(tree);
            if (state == STATUS.FAILED) {
                return STATUS.SUCCEED;
            }
        } else {
            sub.leave(tree);
            return STATUS.SUCCEED;
        }
        return STATUS.RUNNING;
    }
}
